home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch11 / Bezier.frm (.txt) < prev    next >
Visual Basic Form  |  1999-06-12  |  7KB  |  207 lines

  1. VERSION 5.00
  2. Begin VB.Form frmBezier 
  3.    Caption         =   "Bezier"
  4.    ClientHeight    =   5490
  5.    ClientLeft      =   2175
  6.    ClientTop       =   645
  7.    ClientWidth     =   4830
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   366
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   322
  13.    Begin VB.CommandButton cmdGo 
  14.       Caption         =   "Go"
  15.       Default         =   -1  'True
  16.       Height          =   375
  17.       Left            =   4320
  18.       TabIndex        =   4
  19.       Top             =   0
  20.       Width           =   495
  21.    End
  22.    Begin VB.CheckBox chkControlPoints 
  23.       Caption         =   "Show Control Points"
  24.       Height          =   255
  25.       Left            =   1080
  26.       TabIndex        =   3
  27.       Top             =   60
  28.       Value           =   1  'Checked
  29.       Width           =   1815
  30.    End
  31.    Begin VB.TextBox txtDt 
  32.       Height          =   285
  33.       Left            =   240
  34.       TabIndex        =   2
  35.       Text            =   "0.01"
  36.       Top             =   45
  37.       Width           =   615
  38.    End
  39.    Begin VB.PictureBox picCanvas 
  40.       AutoRedraw      =   -1  'True
  41.       Height          =   4815
  42.       Left            =   0
  43.       ScaleHeight     =   317
  44.       ScaleMode       =   3  'Pixel
  45.       ScaleWidth      =   317
  46.       TabIndex        =   0
  47.       Top             =   480
  48.       Width           =   4815
  49.    End
  50.    Begin VB.Label Label1 
  51.       Caption         =   "dt"
  52.       Height          =   255
  53.       Index           =   1
  54.       Left            =   0
  55.       TabIndex        =   1
  56.       Top             =   60
  57.       Width           =   255
  58.    End
  59. Attribute VB_Name = "frmBezier"
  60. Attribute VB_GlobalNameSpace = False
  61. Attribute VB_Creatable = False
  62. Attribute VB_PredeclaredId = True
  63. Attribute VB_Exposed = False
  64. Option Explicit
  65. Private Const GAP = 2
  66. ' The endpoints are points 1 and 4. The control
  67. ' points are points 2 and 3.
  68. Private Const NumPts = 4
  69. Private PtX(1 To NumPts) As Single
  70. Private PtY(1 To NumPts) As Single
  71. ' The index of the point being dragged.
  72. Private Dragging As Integer
  73. ' The Bezier curve parameters.
  74. Private Ax As Single
  75. Private Bx As Single
  76. Private Cx As Single
  77. Private Dx As Single
  78. Private Ay As Single
  79. Private By As Single
  80. Private Cy As Single
  81. Private Dy As Single
  82. ' Draw the curve on the indicated picture box.
  83. Private Sub DrawCurve(ByVal pic As PictureBox, ByVal start_t As Single, ByVal stop_t As Single, ByVal dt As Single)
  84. Dim t As Single
  85.     pic.Cls
  86.     pic.CurrentX = X(start_t)
  87.     pic.CurrentY = Y(start_t)
  88.     t = start_t + dt
  89.     Do While t < stop_t
  90.         pic.Line -(X(t), Y(t))
  91.         t = t + dt
  92.     Loop
  93.     pic.Line -(X(stop_t), Y(stop_t))
  94. End Sub
  95. ' Compute the Bezier curve parameters.
  96. Private Sub GetBezierValues(ByVal ex1 As Single, ByVal ey1 As Single, ByVal ex2 As Single, ByVal ey2 As Single, ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single, ByRef Ax As Single, ByRef Bx As Single, ByRef Cx As Single, ByRef Dx As Single, ByRef Ay As Single, ByRef By As Single, ByRef Cy As Single, ByRef Dy As Single)
  97.     Ax = ex2 - ex1 - 3 * x2 + 3 * x1
  98.     Bx = 3 * ex1 - 6 * x1 + 3 * x2
  99.     Cx = -3 * ex1 + 3 * x1
  100.     Dx = ex1
  101.     Ay = ey2 - ey1 - 3 * y2 + 3 * y1
  102.     By = 3 * ey1 - 6 * y1 + 3 * y2
  103.     Cy = -3 * ey1 + 3 * y1
  104.     Dy = ey1
  105. End Sub
  106. ' The parametric function Y(t).
  107. Private Function Y(ByVal t As Single) As Single
  108.     Y = Ay * t ^ 3 + By * t * t + Cy * t + Dy
  109. End Function
  110. ' The parametric function X(t).
  111. Private Function X(ByVal t As Single) As Single
  112.     X = Ax * t ^ 3 + Bx * t * t + Cx * t + Dx
  113. End Function
  114. ' Prepare to draw the Bezier curve.
  115. Private Sub DrawBezier()
  116. Dim dt As Single
  117. Dim i As Integer
  118.     ' Compute the curve parameters.
  119.     GetBezierValues _
  120.         PtX(1), PtY(1), _
  121.         PtX(4), PtY(4), _
  122.         PtX(2), PtY(2), _
  123.         PtX(3), PtY(3), _
  124.         Ax, Bx, Cx, Dx, Ay, By, Cy, Dy
  125.     ' Draw the curve.
  126.     dt = CSng(txtDt.Text)
  127.     DrawCurve picCanvas, 0, 1, dt
  128.     If chkControlPoints.Value = vbChecked Then
  129.         ' Draw the control points.
  130.         For i = 1 To NumPts
  131.             picCanvas.Line _
  132.                 (PtX(i) - GAP, PtY(i) - GAP)- _
  133.                 Step(2 * GAP, 2 * GAP), , BF
  134.         Next i
  135.         
  136.         ' Connect the control points.
  137.         picCanvas.DrawStyle = vbDot
  138.         picCanvas.CurrentX = PtX(1)
  139.         picCanvas.CurrentY = PtY(1)
  140.         For i = 2 To NumPts
  141.             picCanvas.Line -(PtX(i), PtY(i))
  142.         Next i
  143.         picCanvas.DrawStyle = vbSolid
  144.     End If
  145. End Sub
  146. ' Select a point and start dragging it.
  147. Private Sub picCanvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  148. Dim i As Integer
  149.     ' Find a close point.
  150.     For i = 1 To NumPts
  151.         If Abs(PtX(i) - X) <= GAP And _
  152.            Abs(PtY(i) - Y) <= GAP Then Exit For
  153.     Next i
  154.     If i > NumPts Then Exit Sub
  155.     Dragging = i
  156.     picCanvas.DrawMode = vbInvert
  157.     PtX(Dragging) = X
  158.     PtY(Dragging) = Y
  159.     picCanvas.Line _
  160.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  161.         Step(2 * GAP, 2 * GAP), , BF
  162. End Sub
  163. ' Continue dragging a point.
  164. Private Sub picCanvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  165.     If Dragging < 1 Then Exit Sub
  166.     picCanvas.Line _
  167.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  168.         Step(2 * GAP, 2 * GAP), , BF
  169.     PtX(Dragging) = X
  170.     PtY(Dragging) = Y
  171.     picCanvas.Line _
  172.         (PtX(Dragging) - GAP, PtY(Dragging) - GAP)- _
  173.         Step(2 * GAP, 2 * GAP), , BF
  174. End Sub
  175. ' Finish the drag and redraw the curve.
  176. Private Sub picCanvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  177.     If Dragging < 1 Then Exit Sub
  178.     picCanvas.DrawMode = vbCopyPen
  179.     PtX(Dragging) = X
  180.     PtY(Dragging) = Y
  181.     Dragging = 0
  182.     DrawBezier
  183. End Sub
  184. Private Sub cmdGo_Click()
  185.     DrawBezier
  186. End Sub
  187. Private Sub chkControlPoints_Click()
  188.     DrawBezier
  189. End Sub
  190. Private Sub Form_Load()
  191.     PtX(1) = 0.4 * picCanvas.ScaleWidth
  192.     PtX(2) = 0.1 * picCanvas.ScaleWidth
  193.     PtX(3) = 0.8 * picCanvas.ScaleWidth
  194.     PtX(4) = 0.6 * picCanvas.ScaleWidth
  195.     PtY(1) = 0.8 * picCanvas.ScaleHeight
  196.     PtY(2) = 0.3 * picCanvas.ScaleHeight
  197.     PtY(3) = 0.2 * picCanvas.ScaleHeight
  198.     PtY(4) = 0.7 * picCanvas.ScaleHeight
  199. End Sub
  200. ' Make the picCanvas as big as possible.
  201. Private Sub Form_Resize()
  202.     picCanvas.Move 0, picCanvas.Top, _
  203.         ScaleWidth, ScaleHeight - picCanvas.Top
  204.         
  205.     DrawBezier
  206. End Sub
  207.